package com.ruoyi.web.controller.ams;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ams.entity.DiscountCoupon;
import com.ruoyi.ams.service.IDiscountCouponService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2021-11-02
 */
@RestController
@RequestMapping("/system/coupon")
public class AmsDiscountCouponController extends BaseController {
    @Autowired
    private IDiscountCouponService amsDiscountCouponService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(DiscountCoupon amsDiscountCoupon) {
        startPage();
        List<DiscountCoupon> list = amsDiscountCouponService.list(new QueryWrapper<>(amsDiscountCoupon));
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DiscountCoupon amsDiscountCoupon) {
        List<DiscountCoupon> list = amsDiscountCouponService.list(new QueryWrapper<>(amsDiscountCoupon));
        ExcelUtil<DiscountCoupon> util = new ExcelUtil<DiscountCoupon>(DiscountCoupon.class);
        return util.exportExcel(list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(amsDiscountCouponService.getById(id));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DiscountCoupon amsDiscountCoupon) {
        return toAjax(amsDiscountCouponService.save(amsDiscountCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DiscountCoupon amsDiscountCoupon) {
        return toAjax(amsDiscountCouponService.updateById(amsDiscountCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        return toAjax(amsDiscountCouponService.removeByIds(list));
    }
}